
//获取应用实例
var app = getApp()
var util = require('../util.js')
Page({
  data: {
    openid: "",
    list: [],
    RolePerm: "",
    userInfo: {},

  },
  //事件处理函数
  bindViewTap: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: id
    })
  },
  //获取菜单
  getSceneLists: function () {
    var RolePerm = this.data.RolePerm;
    //console.log(RolePerm);
    var that = this;
    wx.cloud.callFunction({
      name: 'Get_fun',
      data: {
        data: '{"[]":{"K_W_Permission":{"@column":"TypeName,Pid,Name"}}}'
      },
      success: res => {
        let obj = JSON.parse(res.result)["[]"];
        for (let i = 0; i < obj.length; i++) {
          var K_W_Permission = obj[i]["K_W_Permission"]
          //console.log(K_W_Permission.Pid + "|" + RolePerm.indexOf(K_W_Permission.Pid + ","));
          if (RolePerm.indexOf(K_W_Permission.Pid + ",") >= 0) {
            if (K_W_Permission.Pid.indexOf("A") >= 0) {
              var MA = '{"id": ' + (i + 1) + ',"name": "' + K_W_Permission.Name + '","pic_url": "http:\/\/waiqin.obess.com\/jpg\/' + (i + 1) + '.jpg","Pid":"' + K_W_Permission.Pid + '","go_url": "../' + K_W_Permission.TypeName + '/Index?title=' + util.formatDate(new Date()) + K_W_Permission.Name + '"}';
              that.setData({
                list: this.data.list.concat(JSON.parse(MA)),
              });
            }
          }
        }
      },
      fail: err => {
        console.error('[云函数] [login] 调用失败', err);
        wx.showToast({
          icon: 'none',
          title: '调用失败',
        })
      }
    })
  },
  getRolePerm: function () {//获取权限
    var that = this;
    wx.cloud.callFunction({
      name: 'Get_fun',
      data: {
        data: '{"[]":{"K_W_RolePerm":{"TypeName":"' + getApp().globalData.ID_CODE + '","@column":"attributeValue0"}}}'
      },
      success: res => {
        try {
          let obj = JSON.parse(res.result)["[]"][0].K_W_RolePerm;
          that.setData({
            RolePerm: obj.attributeValue0
          });
          app.globalData.pright = obj.attributeValue0;
          this.getSceneLists();//执行获得菜单
        } catch (error) {
          this.onLoad();
          /*wx.showToast({
            icon: 'none',
            title: '请重新进入！',
          })*/
        }
      },
      fail: err => {
        console.error('[云函数] [login] 调用失败', err);
        wx.showToast({
          icon: 'none',
          title: '调用失败',
        })
      }
    })
  },

  onLoad: function () {
    wx.setStorageSync('Login_time', app.globalData.openid)
    this.getRolePerm();
    this.setData({
      openid: app.globalData.openid,
      name: app.globalData.name,
      typename: app.globalData.TypeName,
    })
    wx.showLoading({
      title: '加载中'
    })
    this.GetSignin();
    // this.GetSet();
  },
  //获取签到
  GetSignin: function (e) {
    wx.cloud.callFunction({
      name: 'Get_fun',
      data: {
        data: '{"[]": {"K_W_Signin": { "Openid":"' + getApp().globalData.openid + '","Memo$": "' + util.formatDate(new Date()) + '%"}}}'
      },
      success: res => {
        try {
          let obj = JSON.parse(res.result)["[]"];
          //console.log(obj.length);
          if (obj.length < 1) {
            this.Signin_it();
          };
        } catch (error) {
          wx.showToast({
            icon: 'none',
            title: '调用失败',
          })
        }
      },
      fail: err => {
        //console.error('[云函数] [login] 调用失败', err);
        wx.showToast({
          icon: 'none',
          title: '调用失败',
        })
      }
    })
  },
  ///签到
  Signin_it: function (e) {
    wx.showModal({
      title: '签到提示',
      content: '嘿，今天还没签到，\n是否立刻去签到',
      success(res) {
        if (res.confirm) {
          wx.navigateTo({
            url: '../sign/Index'
          })
        }
      }
    })   
  },
  onReady: function () {
    setTimeout(function () {
      wx.hideLoading();
    }, 500)
  },

})